﻿using System;
using System.Windows;
using System.Windows.Input;
using Quizzer.Model;
using Quizzer.Helpers;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Quizzer.DataAccess;

namespace Quizzer.ViewModel
{
    [Magic]
    public class LoginViewModel : ViewModelBase
    {
        public LoginViewModel()
        {
            GoToRegistrationPage = new RelayCommand(() => ExecuteGoToRegistrationPage());
            LogOn = new RelayCommand(() => ExecuteLogOn(),
                ()=>( !(string.IsNullOrEmpty(Username)) && !(string.IsNullOrEmpty(Password)) ));

            ErrorUsernameVisible = Visibility.Collapsed;   // hide any error messages
            ErrorPasswordVisible = Visibility.Collapsed;

            Username = "mark";      // just for quicker login. delete it when need
            Password = "mark";
        }

        #region Properties
        
        public string Username { get; set; }
        public string Password { get; set; }
        public Visibility ErrorUsernameVisible { get; set; }
        public Visibility ErrorPasswordVisible { get; set; }

        #endregion

        #region Commands
        
        public ICommand LogOn { get; private set; }
        private void ExecuteLogOn()
        {
            ErrorUsernameVisible = Visibility.Collapsed;   // hide any error messages
            ErrorPasswordVisible = Visibility.Collapsed;

            if (AccountRepository.ContainsUsername(Username))
            {
                if (AccountRepository.CanLogin(Username, Password))
                {
                    ErrorUsernameVisible = Visibility.Collapsed;
                    ErrorPasswordVisible = Visibility.Collapsed;

                    // DONT USE THIS. THIS METHOD INVOKES THE CONSTRUCTOR TWICE
                    //Messenger.Default.Send<ViewModelBase, MainViewModel>(new CoursesViewModel()); 
                    
                    // Navigate to courses page
                    Messenger.Default.Send<string, MainViewModel>("CoursesPage");
                    Messenger.Default.Send<Account, CoursesViewModel>(AccountRepository.GetAccount(Username, Password));

                    Username = "";  // clear fields
                    Password = "";
                    return;
                }
                else
                {
                    ErrorPasswordVisible = Visibility.Visible;
                    return;
                }
            }
            ErrorUsernameVisible = Visibility.Visible;
        }

        public ICommand GoToRegistrationPage { get; private set; }
        private void ExecuteGoToRegistrationPage()
        {
            // DONT USE THIS. THIS METHOD INVOKES THE CONSTRUCTOR TWICE
            //Messenger.Default.Send<ViewModelBase, MainViewModel>(new RegistrationViewModel());
            Messenger.Default.Send<string, MainViewModel>("RegistrationPage"); 
            
            Username = "";
            Password = "";
        }

        #endregion
    }
}
